package com.liming.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liming.according.According4Shops;
import com.liming.model.Goods;
import com.liming.model.GoodsInfo;
import com.liming.model.Shop;
import com.liming.utils.DataSourceUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;


import sun.net.www.content.image.gif;


/**  
* <p>Title: ShopDao.java</p>  
* <p>Description: </p>  
* <p>Copyright: Copyright (c) 2017</p>  
* <p>Company: 黎明</p>  
* @author ltf 
* @date 2019年6月27日  
* @version 1.0  
*/  
public class ShopDao {
	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	/**
	 * 通过id获取shop
	 * @param shopid
	 * @return
	 */
	public Shop getShop(int shopid) {
		String sql="select * from shop where shopid=?";
		Shop a=new Shop();
		try {
			a=(Shop) qr.query(sql, new BeanHandler(Shop.class),shopid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}
	/**
	 * 按热度 或信誉 从大到小排序 列出店铺信息
	 * @param according
	 * @return
	 */
	public List<Shop> getShops(According4Shops according){
		List<Shop> aList=null;
		String sql="select * from shop order by "+according.getOrderAccording()+" desc";
		System.out.println(sql);
		try {
			aList=qr.query(sql,  new BeanListHandler<>(Shop.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return aList;
	}
	
	/**
	 * 更新商店信息
	 */
	public int updateShop(Shop s) {
		String sql="update shop set shopname=?,shopdesc=? where shopid=?";
		int flag=0;
		try {
			flag=qr.update(sql,s.getShopname(),s.getShopdesc(),s.getShopid());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 上传商品信息
	 * @param s
	 * @param g
	 * @param gI
	 * @return
	 */
	public int publishGoods(int shopid,Goods g,GoodsInfo gI) {
		String sql="insert into goods(goodsname,typeid,price,salenum,shopid,goodsstatus,ishot,goodsrc) values(?,?,?,0,?,?,0,?)";
		String sql2="insert into goodsinfo(goodsdesc,goodsnum,goodsclass) values(?,?,?)";
		int flag=0;
		int flag1=0;
		try {
			flag=qr.update(sql,g.getGoodsname(),g.getTypeid(),g.getPrice(),shopid,g.getGoodsstatus(),g.getGoodsrc());
			flag1=qr.update(sql2,gI.getGoodsdesc(),gI.getGoodsnum(),gI.getGoodsclass());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag==1&&flag1==1) {
			return 1;
		}else {
			return 0;
		}
	}
	/**
	 * 关闭店铺 把店铺状态修改为 0  然后将这个店铺的商品的状态上架状态也修改为 0
	 * @param s
	 */
	public int closeShop(int shopid) {
		String sql="update shop set shopstatus=0 where shopid = ?";
		String sql2="update goods set goodsstatus=0 where shopid =?";
		int flag=0;
		int flag1=0;
		try {
			flag=qr.update(sql,shopid);
			flag1=qr.update(sql2,shopid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (flag!=0&&flag1!=0) {
			return 1;
		}
		return 0;
	}
	public int openShop(Shop s, String username) {
		String sql="insert into shop(shopname,shopdesc,shopstatus) values(?,?,1)";//插入商店信息  
		String sql2="select shopid from shop where shopname=?";
		String sql3="update user set shopid=? where username=?";
		int flag1=0;
		int shopid=0;
		int flag3=0;
		try {
			flag1=qr.update(sql,s.getShopname(),s.getShopdesc());//添加商店信息
			shopid=(int) qr.query(sql2, new ScalarHandler(),s.getShopname());
			flag3=qr.update(sql3, shopid,username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag1!=0&&flag3!=0) {
			return 1;
		}
		return 0;
	}
}
